﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
using CI.Mesopotamia;
using MessageBox = System.Windows.Forms.MessageBox;

namespace CI.PW
{
    public class PWBootStrap : BootStrap
    {
        public Progress ProgressWindow;
        public event Action ManifestStarted;
        private Display _display;
        private Thread _simulationThread;

        protected override void OnManifestStarted()
        {
            if (_display != null)
                _display.Dispatcher.BeginInvoke(DispatcherPriority.Send, (Action)(() => _display.SetTitleScenarioName()));


            if (ManifestStarted != null)
                ManifestStarted();
        }

        protected override void RoboticsEngineStarting()
        {
            ProgressWindow.Dispatcher.BeginInvoke(DispatcherPriority.Send, (Action)delegate
                                       {
                                           ProgressWindow.Show();

                                           if (Globals.LaunchedFrom == LaunchType.ScreenSaver)
                                               ProgressWindow.Visibility = Visibility.Collapsed;
                                       });
        }

        protected override void ReportError(string message, string caption)
        {
            if (!WasRestart)
            {
                MessageBox.Show(message, caption);
                SimEnv.Instance.ForceCleanExit = true;
            }
        }

        protected override void PreScenarioHostBootstrap()
        {
            ProgressWindow.Dispatcher.BeginInvoke(DispatcherPriority.Send, (Action)delegate
                                       {
                                           ProgressWindow.Visibility = Visibility.Collapsed;
                                       });
        }

        protected override void RestartPreCleanUp()
        {
            ProgressWindow.Dispatcher.BeginInvoke(DispatcherPriority.Send, (Action)delegate
                                       {
                                           ProgressWindow.Visibility = Visibility.Visible;
                                       });
        }

        protected override LaunchType StartupMode()
        {
            _simulationThread = new Thread(() =>
            {
                _simulationThread.IsBackground = true;
                _display = new Display(SimEnv.Instance, this, WasRestart);
                _display.ShowDialog();
                _display.Close();
                SimEnv.Instance.TraceLogger.LogDebug("Display thread exited");
                _display = null;
                _simulationThread = null;
            });

            _simulationThread.TrySetApartmentState(ApartmentState.STA);
            _simulationThread.Start();

            return Globals.LaunchedFrom;
        }

        protected override void OnStartup()
        {
            return;
        }
    }
}
